

#import "DlHrSatisfaction.h"
#import "Networking.h"
#import "InitDbTable.h"
#import "UserDefault.h"
#import <TMCache.h>

@implementation DlHrSatisfaction

-(instancetype)init{
    
    NSString *url = AppServiceConfig.GetServiceUrl;
    self.downloadUrl  = [NSString stringWithFormat:@"%@/%@?token=%@",url,@"GetSaData",[[TMCache sharedCache] objectForKey:@"deviceToken"]];
    NSLog(@"%@", self.downloadUrl);
    return self;
}

-(void) startDownload:(void(^)())callback{
    NSLog(@"开始 下载满意度调查基础数据记录");
    
    [Networking receiveDataWithURLString:self.downloadUrl method:@"GET" body:nil Block:^(NSData *tempData) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        InitDbTable* initdb = [[InitDbTable alloc]init];
        
        
        Boolean flag =  [[dic valueForKey:@"flag"] boolValue];
        NSString *msg = (NSString*)[dic objectForKey: @"msg"];
        
        if(! flag ){
            NSLog(@"%@",msg);
            return;
        }
        
        NSDictionary *data = [dic objectForKey: @"data"];
        
        NSDictionary *sa_type = [data objectForKey: @"sa_type"];
        NSDictionary *sa_code = [data objectForKey: @"sa_code"];
        NSDictionary *sa_dept = [data objectForKey: @"sa_dept"];
        NSDictionary *sa_item = [data objectForKey: @"sa_item"];
        
        NSMutableArray *arr_sa_type = [[NSMutableArray alloc]init];
        NSMutableArray *arr_sa_code = [[NSMutableArray alloc]init];
        NSMutableArray *arr_sa_dept = [[NSMutableArray alloc]init];
        NSMutableArray *arr_sa_item = [[NSMutableArray alloc]init];
        
        NSLog(@"下载成功");
        NSLog(@"共%lu条满意度类型记录数据",(unsigned long)[sa_type count]);
        NSLog(@"共%lu条满意度指标记录数据",(unsigned long)[sa_code count]);
        NSLog(@"共%lu条满意度科室记录数据",(unsigned long)[sa_dept count]);
        NSLog(@"共%lu条满意度调查项记录数据",(unsigned long)[sa_item count]);
        
        for (NSDictionary *diction in sa_type) {
            Model_Hr_SatType *model =[[Model_Hr_SatType alloc]initWithDictionary:diction];
            [arr_sa_type addObject:model];
        }
        for (NSDictionary *diction in sa_code) {
            Model_Hr_SatCode *model =[[Model_Hr_SatCode alloc]initWithDictionary:diction];
            [arr_sa_code addObject:model];
        }
        for (NSDictionary *diction in sa_dept) {
            Model_Hr_SatDept *model =[[Model_Hr_SatDept alloc]initWithDictionary:diction];
            [arr_sa_dept addObject:model];
        }
        
        for (NSDictionary *diction in sa_item) {
            Model_Hr_SatCodeItem *model =[[Model_Hr_SatCodeItem alloc]initWithDictionary:diction];
            [arr_sa_item addObject:model];
        }
        
        
        NSLog(@"正在清除原有满意度调查基础数据");
        [initdb deleteHrSatisfcation];
        
        
        NSLog(@"正在写入满意度基础数据");
        [initdb insertHrSatisfcation:arr_sa_code arrSaType:arr_sa_type arrSaDept:arr_sa_dept arrSaItem:arr_sa_item];
        
        if(callback){
            callback();
        }
        
        NSLog(@"完成 下载满意度基础数据");
        
    }];
    
}


@end
